package com.zide.system.controller;

import com.zide.common.annotation.Log;
import com.zide.common.core.domain.CommonResult;
import com.zide.common.core.domain.PageResult;
import com.zide.common.enums.BusinessType;
import com.zide.common.utils.poi.ExcelUtil;
import com.zide.system.domain.post.SysPost;
import com.zide.system.domain.post.vo.SysPostResultVO;
import com.zide.system.domain.post.vo.SysPostSearchVO;
import com.zide.system.service.ISysPostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zide.common.core.domain.CommonResult.*;

/**
 * 岗位信息操作处理
 *
 * @author zide
 */
@Tag(name = "岗位管理")
@RestController
@RequestMapping("/system/post")
public class SysPostController {
    @Autowired
    private ISysPostService postService;

    @Operation(summary = "获取岗位列表")
    @GetMapping("/list")
    public CommonResult list(SysPostSearchVO searchVO) {
        PageResult<SysPostResultVO> list = postService.searchPage(searchVO);
        return success(list);
    }

    @Operation(summary = "导出岗位")
    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPost post) {
        List<SysPost> list = postService.selectPostList(post);
        ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class);
        util.exportExcel(response, list, "岗位数据");
    }

    @Operation(summary = "根据岗位编号获取详细信息")
    @GetMapping(value = "/{postId}")
    public CommonResult getInfo(@PathVariable Long postId) {
        return success(postService.selectPostById(postId));
    }

    @Operation(summary = "新增岗位")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public CommonResult add(@Validated @RequestBody SysPost post) {
        if (!postService.checkPostNameUnique(post)) {
            return error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (!postService.checkPostCodeUnique(post)) {
            return error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        return getResult(postService.insertPost(post));
    }

    @Operation(summary = "修改岗位")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public CommonResult edit(@Validated @RequestBody SysPost post) {
        if (!postService.checkPostNameUnique(post)) {
            return error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (!postService.checkPostCodeUnique(post)) {
            return error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        return getResult(postService.updatePost(post));
    }

    @Operation(summary = "删除岗位")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public CommonResult remove(@PathVariable Long[] postIds) {
        return getResult(postService.deletePostByIds(postIds));
    }

    @Operation(summary = "获取岗位选择框列表")
    @GetMapping("/optionselect")
    public CommonResult optionselect() {
        List<SysPost> posts = postService.selectPostAll();
        return success(posts);
    }
}
